package com.mediacloud.app.newsmodule.fragment.audio.live;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.mediacloud.app.model.news.LiveProgramListReciver;
import com.mediacloud.app.model.news.ProgramListItem;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.fragment.video.live.LiveProgramTableFragment;
import com.mediacloud.app.newsmodule.model.SimpleNavigateItem;
import com.mediacloud.app.reslib.util.AppFontToolKt;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 音频直播节目单管理器
 *
 * @author x
 */
public class AudioLiveProgramTable2 extends LiveProgramTableFragment {

    @Override
    protected void getProgramList() {
    }


    @Override
    public void setDatePrograms(List<LiveProgramListReciver.ProgramWeekItem> programListItems) {
        if (navigateItems == null)
            navigateItems = new ArrayList<>();
        navigateItems.clear();
        middle = (programListItems.size() - 1) / 2;
        for (int i = 0; i < programListItems.size(); i++) {
            SimpleNavigateItem catalogItem = new SimpleNavigateItem();
            catalogItem.index = i;
            if (i == middle) {
                catalogItem.name = getWeek(programListItems.get(i).getWeek()) + "\n" + getResources().getString(R.string.today);
            } else {
                catalogItem.name = getWeek(programListItems.get(i).getWeek()) + "\n" + getDate(programListItems.get(i).getTitle());
            }
            navigateItems.add(catalogItem);
        }
    }

    private String getDate(String date) {
        String[] datas = date.split("月");
        if (datas.length > 1) {
            String[] datass = datas[1].split("日");
            if (datass.length > 0) {
                return datass[0];
            } else {
                return date;
            }
        }
        return date;
    }

    private String getWeek(String week) {
        if ("0".equals(week)) {
            return "周日";
        } else if ("1".equals(week)) {
            return "周一";
        } else if ("2".equals(week)) {
            return "周二";
        } else if ("3".equals(week)) {
            return "周三";
        } else if ("4".equals(week)) {
            return "周四";
        } else if ("5".equals(week)) {
            return "周五";
        } else {
            return "周六";
        }
    }
    @Override
    public Fragment getViewContentFragment(SimpleNavigateItem catalogItem) {
        AudioProgramItemFragment fragment = new AudioProgramItemFragment();
        fragment.liveTableIndex = catalogItem.index;
        Bundle bundle = new Bundle();
        bundle.putParcelable("data", catalogItem);
        bundle.putBoolean("chatStyle", true);


        mCatalogItemContainer.setIndicatorHeight(0);
        mCatalogItemContainer.setTextUnselectColor(0xff333333);
        mCatalogItemContainer.setTextSelectColor( 0xff333333);
        mCatalogItemContainer.setMinimumHeight((int) getContext().getResources().getDimension(R.dimen.dimen46));
//        mCatalogItemContainer.addOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(XTabLayout.Tab tab) {
//                try {
//                    GradientDrawable gradientDrawable = new GradientDrawable();
//
//                    gradientDrawable.setColor(0xFFF1EFF2);
//                    if (tab.getCustomView() == null) {
//                        TextView tv = getTextView();
//                        tab.setCustomView(tv);
//                    }
//                    TextView tv = tab.getCustomView().findViewById(android.R.id.text1);
//                    tv.setBackground(gradientDrawable);
//                    tv.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
//                    tv.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
//                    tv.setGravity(Gravity.CENTER);
//                    tv.setPadding(10, 0, 10, 10);
////                    SpannableString ss = new SpannableString(tab.getText());
////                    RelativeSizeSpan sizeSpan = new RelativeSizeSpan(0.8f);
////                    ss.setSpan(sizeSpan, 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
////                    tv.setText(ss);
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onTabUnselected(XTabLayout.Tab tab) {
//                try {
//                    GradientDrawable gradientDrawable = new GradientDrawable();
//
//                    gradientDrawable.setColor(0xFFF1EFF2);
//                    if (tab.getCustomView() == null) {
//                        TextView tv = getTextView();
//                        tab.setCustomView(tv);
//                    }
//                    TextView tv = tab.getCustomView().findViewById(android.R.id.text1);
//                    tv.setBackground(null);
//                    tv.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
//                    tv.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
//                    tv.setGravity(Gravity.CENTER);
//                    tv.setPadding(10, 0, 10, 10);
////                    SpannableString ss = new SpannableString(tab.getText());
////                    RelativeSizeSpan sizeSpan = new RelativeSizeSpan(0.8f);
////                    ss.setSpan(sizeSpan, 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
////                    tv.setText(ss);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onTabReselected(XTabLayout.Tab tab) {
//
//            }
//        });


        if (programListItems != null && programListItems.size() == navigateItems.size()) {
            List<ProgramListItem> programList = programListItems.get(catalogItem.index).programList;
            if (programList == null)
                programList = new ArrayList<ProgramListItem>();
            bundle.putSerializable(Program, (Serializable) programList);
        }
        fragment.setArguments(bundle);
        return fragment;
    }

    @NotNull
    private TextView getTextView() {
        TextView tv = new TextView(getContext());
        try {
//            tv.setTypeface(ResourcesCompat.getFont(getContext(),R.font.app_font));
            AppFontToolKt.setTextViewFont(tv);
        } catch (Exception e) {
        }
        tv.setHeight((int) getResources().getDimension(R.dimen.dimen22));
        tv.setWidth((int) getResources().getDimension(R.dimen.dimen45));
        tv.setId(android.R.id.text1);
        tv.setTextSize(15);
        tv.setGravity(Gravity.CENTER);
        return tv;
    }

    @Override
    public int getLayoutResID() {
        return R.layout.aappfactory_livedetail_fragment_chat;
    }

    View.OnClickListener clickListener;

    public void setOnCloseBtnClickListenser(View.OnClickListener clickListener) {
        this.clickListener = clickListener;
    }
}